<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/3/2
 * Time: 上午9:24
 */
namespace Home\Controller;
use Think\Controller;
class ClassificationController extends Controller
{
    public function lists()
    {
        //获取分类ID
        $id = I('get.id',0);
        if(empty($id)){
            $this->error('非法操作');
        }
        //获取页数
        $page = I('get.page',1);
        $all_num = 3;
        $start_num = (empty($page) || $page == 1) ? 0 : ($page-1) * $all_num;
        //获取排序类别
        $sorts = I('get.sort','');
        //默认排序为空
        $order = '';

        if(!empty($sorts)){
            //如果不为空则获取排序规则
            $orders = I('get.order','');
            if(empty($orders)){
                $this->error('请不要乱玩！');
            }
            if($sorts == 'price'){
                $order = 'fd_price '.$orders;
            }elseif($sorts == 'time'){
                $order = 'add_time '.$orders;
            }elseif($sorts == 'sale_num'){
                $order = $sorts.' '.$orders;
            }else{
                $this->error('请不要乱搞！');
            }
        }

        //获取分类
        $ids = D('classification')->get_ids($id);
        $where = 'class_id in ('.$id.$ids.')';
        //获取品牌
        $brand_id = I('get.brand',0);
        if(!empty($brand_id)){
            $where .= ' and bid='.$brand_id;
        }
        //获取价格区间
        $price_min = I('get.price_min',0);
        $price_max = I('get.price_max',0);
        if($price_max < $price_min){
            $this->error('你在犯逗吗？');
        }
        if(!empty($price_max)){
            $where .= ' and fd_price<'.$price_max.' and fd_price>'.$price_min;
        }
        $field = 'id,goods_name,litimg,market_price,fd_price,description';
        $limit = $start_num.','.$all_num;
        //获取当前分类所有父级信息及自己的信息
        $p_info = D('classification')->get_all_p($id);
        //获取当前分类所在一级分类下的所有分类
        if('' == $ids){
            $all_child = D('classification')->get_all_c($p_info[2]['id']);
        }elseif(count($p_info,0) == 2){
            $all_child = D('classification')->get_all_c($p_info[1]['id']);
        }else{
            $all_child = D('classification')->get_all_c($id);
        }
        $goods_info = D('goods')->get_many($where,$field,$order,$limit);
        $count = D('goods')->get_many($where,'count(*) as num');
        $page_num = ceil($count[0]['num']/$all_num);
        //获取所有品牌
        $brand_info = D('Brand')->getField('id,brand_name');
        //生成商品分类树
        $classification = D('Classification');
        $classification_info = $classification->getTreeData('level','id','classification');
        $this->assign('classification_info',$classification_info);
        $this->assign('count',$count[0]['num']);    //所有商品数量
        $this->assign('now_page',$page);            //当前页面
        $this->assign('page_num',$page_num);        //总页数
        $this->assign('goods_info',$goods_info);    //当前页面商品信息
        $this->assign('p_info',$p_info);            //当前分类所在祖辈信息
        $this->assign('all_child',$all_child);      //当前分类所在一级分类下的所有孩子分类信息
        $this->assign('brand_info',$brand_info);    //所有品牌信息
        $this->assign('class_id',$id);
        $this->display();
    }
}